﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace SuperX.Helper.Validation
{
    /// <summary>
    /// 检查的基类
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public abstract class ValidationAttribute : Attribute
    {
        private string Message;

        public string ErrorMessage
        {
            get { return string.IsNullOrEmpty(Message) ? "属性验证未通过！" : Message; }
            set { Message = value; }
        }

        public virtual string IsValid(object value)
        {
            return value == null ? ErrorMessage : null;
        }
    }
    /// <summary>
    /// 非空判断
    /// </summary>
    public class EmptyAttribute : ValidationAttribute
    {
        public override string IsValid(object value)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()))
                throw new Exception(ErrorMessage);
            return null;
        }
    }
}
